package com.yksj.healthtalk.ui.buyandsell;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;

public class BuyAndSellMainActivity extends BaseFragmentActivity implements
		OnClickListener {

	private boolean booleanExtra;// true 表示我买 false我卖
	String type = "";// 1 表示我买 2我卖

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.buy_and_seller_main_ui);
		initView();
		initData();
	}

	private void initView() {
		initTitle();
		booleanExtra = getIntent().getBooleanExtra("type", true);

		if (booleanExtra) {
			titleTextV.setText("我的购买");
		} else {
			titleTextV.setText("我卖");
		}

		if (getIntent().hasExtra("title")) {
			titleTextV.setText(getIntent().getStringExtra("title"));
			if (getIntent().getStringExtra("title").equals("收入统计")) {
				((TextView) findViewById(R.id.service)).setText("服务收入");
			}
		}
		titleLeftBtn.setOnClickListener(this);
		findViewById(R.id.salon_in).setOnClickListener(this);
		
		if((SmartFoxClient.getLoginUserInfo().isDoctor())||booleanExtra){
			findViewById(R.id.doctor_in).setOnClickListener(this);
			if(!booleanExtra){
				findViewById(R.id.goods_in).setVisibility(View.VISIBLE);
			}
		}else{
			findViewById(R.id.doctor_in).setVisibility(View.GONE);
		}
		
		findViewById(R.id.goods_in).setOnClickListener(this);
		findViewById(R.id.orider_commodity).setOnClickListener(this);

	}

	private void initData() {
		if (booleanExtra) {
			findViewById(R.id.orider_commodity).setVisibility(View.VISIBLE);
			type = "1";
		} else {
			type = "2";
			findViewById(R.id.orider_commodity).setVisibility(View.GONE);
		}
		HttpRestClient.doHttpBuyandSeller(SmartFoxClient.getLoginUserId(),
				type, new AsyncHttpResponseHandler(this) {
					@Override
					public void onSuccess(int statusCode, String content) {
						super.onSuccess(statusCode, content);
						TextView mAllMoney = (TextView) findViewById(R.id.all_money);
						TextView mSalonNumber = (TextView) findViewById(R.id.salon_number);
						TextView mDoctorNumber = (TextView) findViewById(R.id.doctor_number);
						TextView mCommodity_number = (TextView) findViewById(R.id.commodity_number);
						TextView mGoodsNumber = (TextView) findViewById(R.id.goods_number);
						JSONObject object;
						try {
							object = new JSONObject(content);
							if (booleanExtra) {
								mAllMoney.setText("支出总额:"+ object.optString("TOTAL","0") + "元");
							} else {
								mAllMoney.setText("收入总额:"+ object.optString("TOTAL","0") + "元");
							}
							JSONArray array = object.getJSONArray("INFO");
							// 如果修改字 请通知后台
							for (int i = 0; i < array.length(); i++) {
								JSONObject obj = (JSONObject) array.get(i);
								String type = obj.optString("TYPE").toString();
								if ("医生服务".contains(type)) {
									mDoctorNumber.setText("数量 :"+ obj.optString("COUNT","0"));
								} else if ("话题门票".contains(type)) {
									mSalonNumber.setText("数量 :"+ obj.optString("COUNT","0"));
								} else if ("商品订单".contains(type)) {
									mCommodity_number.setText("数量 :"+ obj.optString("COUNT","0"));
								} else if ("商品推荐".contains(type)) {
									mGoodsNumber.setText("数量 :"+obj.optString("COUNT","0"));
								} else if ("服务收入".contains(type)) {
									mDoctorNumber.setText("数量 :"+ obj.optString("COUNT","0"));
								}
							}
						} catch (JSONException e) {
						}
					}
				});
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.salon_in:
			intent = new Intent(this, BuyOrSellInfoActivity.class);
			intent.putExtra("titleName", "话题门票");
			intent.putExtra("type", type);
			intent.putExtra("FILETYPE", "6");
			startActivity(intent);
			break;
		case R.id.doctor_in:
			intent = new Intent(this, BuyOrSellInfoActivity.class);
			intent.putExtra("titleName", "医生服务");
			intent.putExtra("type", type);
			intent.putExtra("FILETYPE", "5");
			startActivity(intent);
			break;
		case R.id.goods_in:// 医生推荐
			intent = new Intent(this, RecommendationActivity.class);
			startActivity(intent);
			break;
		case R.id.orider_commodity:// 商品订单
			intent = new Intent(this, OrdersforgoodsActivity.class);
			startActivity(intent);
			break;
		}

	}
}
